/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Breed;
import model.Database;

/**
 *
 * @author JArik
 */
public class Breeds {


    public static ArrayList<Breed> getByName(String name) throws SQLException
    {
        Database db = new Database();
        ResultSet set = db.getDBInfo("id, name", "breeds",
                "name LIKE '"+name+"%' AND deleted=0 ORDER BY name != '"+name+"',name");
        ArrayList<Breed> list = new ArrayList<Breed>();
        while ( set.next())
        {
            Breed brd = new Breed();
            brd.setId(set.getInt("id"));
            brd.setName(set.getString("name"));
            list.add(brd);
        }
        return list;
    }
        public static ArrayList<Breed> getByStrictName(String name) throws SQLException
    {
        Database db = new Database();
        ResultSet set = db.getDBInfo("id, name", "breeds",
                "name LIKE '"+name+"' AND deleted=0 ORDER BY name DESC ");
        ArrayList<Breed> list = new ArrayList<Breed>();
        while ( set.next())
        {
            Breed brd = new Breed();
            brd.setId(set.getInt("id"));
            brd.setName(set.getString("name"));
            list.add(brd);
        }
        return list;
    }
    public static int addToCatalog(Breed brd) throws SQLException {
        Database db = new Database();
        int res=0;
        if(brd== null || brd.getName().isEmpty())
            throw new SQLException("Введите название породы");
        ResultSet set = db.insertDBInfo("breeds",
                "name", "'"+brd.getName() +"'");
        if(set.next())
            res= set.getInt(1);
        db.closeStatement();
        return res;
    }
}
